# -*- coding: utf-8 -*-

import pygame
from pygame import Rect
from pygame.locals import *
from sys import exit

import images
from constant import *
from game import Game
from mainmenu import MainMenu
from optionmenu import OptionMenu
from pausemenu import PauseMenu
from dificultymenu import DificultyMenu
from soundmenu import SoundMenu
from lostgamemenu import LostGameMenu
from wingamemenu import WinGameMenu
from helpmenu import HelpMenu
import menuOptions

class Main:
    def __init__(self) :
        pygame.init()
        
        self.screen = pygame.display.set_mode((SCREEN_W,SCREEN_H), 0, 32)
        pygame.display.set_caption('Jogo Senha')
        images.init()
        self.action = None
        self.mainmenu = MainMenu(self)
        self.optionmenu = OptionMenu(self)
        self.dificultymenu = DificultyMenu(self)
        self.soundmenu = SoundMenu(self)
        self.pausemenu = PauseMenu(self)
        self.lostgamemenu = LostGameMenu(self)
        self.wingamemenu = WinGameMenu(self)
        self.helpmenu = HelpMenu(self)
        self.game = None
        self.setGameDificulty(menuOptions.FACIL)
        
    def setAction(self, action) :
        self.action = action
        
    def process_MainMenu (self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.mainmenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.mainmenu.process_MouseClick(x, y)        
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.mainmenu.process_MouseMotion(x, y)  
        self.mainmenu.paint(self.screen)
        
    def process_PauseMenu (self) :
        for event in pygame.event.get() :           
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.pausemenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.pausemenu.process_MouseClick(x, y)        
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.pausemenu.process_MouseMotion(x, y)  
        self.pausemenu.paint(self.screen)        
        
    def process_OptionMenu (self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.optionmenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.optionmenu.process_MouseClick(x, y)
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.optionmenu.process_MouseMotion(x, y)  
        self.optionmenu.paint(self.screen)        

    def process_DificultyMenu (self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.dificultymenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.dificultymenu.process_MouseClick(x, y)                
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.dificultymenu.process_MouseMotion(x, y)  
        self.dificultymenu.paint(self.screen)   
        
    def process_SoundMenu (self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.soundmenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.soundmenu.process_MouseClick(x, y)                
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.soundmenu.process_MouseMotion(x, y)  
        self.soundmenu.paint(self.screen)         

    def process_Game(self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)
            if event.type == KEYDOWN and event.key == K_ESCAPE :
                self.setAction(PAUSE)
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.game.process()
                self.game.newRound()
            if event.type == MOUSEBUTTONDOWN :
                buttons = pygame.mouse.get_pressed()
                x, y = pygame.mouse.get_pos()
                if buttons[0] == 1 and buttons[2] == 0 :
                    self.game.previousColor(x, y)
                elif buttons[0] == 0 and buttons[2] == 1 :
                    self.game.nextColor(x, y)
        
        self.game.paint(self.screen)
        if self.game.hasWon() :
            self.setAction(FIM_JOGO_WIN)
        elif self.game.hasLost() :
            self.setAction(FIM_JOGO_LOST)
            
    def process_WinGameMenu(self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.wingamemenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.wingamemenu.process_MouseClick(x, y)                
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.wingamemenu.process_MouseMotion(x, y)  
        self.game.__lastWinPaint__(self.screen)
        self.wingamemenu.paint(self.screen) 

    def process_LostGameMenu(self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.lostgamemenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.lostgamemenu.process_MouseClick(x, y)                
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.lostgamemenu.process_MouseMotion(x, y)  
        self.game.__lastLostPaint__(self.screen)
        self.lostgamemenu.paint(self.screen)

    def process_HelpMenu(self) :
        for event in pygame.event.get() :
            if event.type == QUIT :
                exit(0)            
            if event.type == KEYDOWN and event.key == K_RETURN :
                self.helpmenu.process_KeyReturn()                
            if event.type == MOUSEBUTTONDOWN :
                x, y = pygame.mouse.get_pos()
                self.helpmenu.process_MouseClick(x, y)                
            if event.type == MOUSEMOTION :
                x, y = pygame.mouse.get_pos()
                #self.helpmenu.process_MouseMotion(x, y)  
        self.helpmenu.paint(self.screen)
        
    def setGameDificulty(self, dificulty) :
        self.game_dificulty = dificulty
        
    def __setGame__(self) :
        max_balls, max_colors, max_rounds = 4, 4, 10
        if self.game_dificulty == menuOptions.FACIL :
            max_balls = 4
            max_colors = 4
            max_rounds = 10
        elif self.game_dificulty == menuOptions.MEDIO :
            max_balls = 4
            max_colors = 6
            max_rounds = 8
        elif self.game_dificulty == menuOptions.DIFICIL :
            max_balls = 5
            max_colors = 7
            max_rounds = 10            
        elif self.game_dificulty == menuOptions.MUITO_DIFICIL :
            max_balls = 6
            max_colors = 8
            max_rounds = 12
        self.game = Game(max_balls, max_colors, max_rounds)
        
    def process(self) :
        while True :
            if self.action == None:
                self.process_MainMenu()
            elif self.action == menuOptions.NOVO_JOGO :
                self.__setGame__()
                self.setAction(menuOptions.AJUDA)
                self.mainmenu.setOption(None)
            elif self.action == menuOptions.OPCOES :
                self.mainmenu.setOption(None)
                self.process_OptionMenu()
            elif self.action == menuOptions.SAIR :
                exit(0)
            elif self.action == menuOptions.DIFICULDADE :
                self.optionmenu.setOption(None)
                self.process_DificultyMenu()
            elif self.action == menuOptions.SOM :
                self.optionmenu.setOption(None)
                self.process_SoundMenu()
                pass
            elif self.action == menuOptions.VOLTAR :
                self.optionmenu.setOption(None)
                self.setAction(None)
            elif self.action == menuOptions.CONTINUAR_JOGO :
                self.setAction(JOGO)
            elif self.action == menuOptions.SAIR_JOGO :
                self.mainmenu.setOption(None)
                self.setAction(None)
            elif self.action == menuOptions.FACIL :
                self.setAction(menuOptions.OPCOES)
                self.setGameDificulty(menuOptions.FACIL)
            elif self.action == menuOptions.MEDIO :
                self.setAction(menuOptions.OPCOES)
                self.setGameDificulty(menuOptions.MEDIO)
            elif self.action == menuOptions.DIFICIL :
                self.setAction(menuOptions.OPCOES)
                self.setGameDificulty(menuOptions.DIFICIL)
            elif self.action == menuOptions.MUITO_DIFICIL :
                self.setAction(menuOptions.OPCOES)
                self.setGameDificulty(menuOptions.MUITO_DIFICIL)                
            elif self.action == menuOptions.LIGADO :
                self.setAction(menuOptions.OPCOES)
            elif self.action == menuOptions.DESLIGADO :
                self.setAction(menuOptions.OPCOES) 
            elif self.action == menuOptions.MENU_PRINCIPAL :
                self.wingamemenu.setOption(None)
                self.lostgamemenu.setOption(None)
                self.setAction(None)
            elif self.action == JOGO :
                self.process_Game()
            elif self.action == PAUSE :
                self.process_PauseMenu()
            elif self.action == FIM_JOGO_WIN :
                self.process_WinGameMenu()                
            elif self.action == FIM_JOGO_LOST :
                self.process_LostGameMenu()   
            elif self.action == menuOptions.AJUDA :
                self.helpmenu.setDificulty(self.game_dificulty)
                self.process_HelpMenu()
            elif self.action == menuOptions.JOGAR :
                self.process_Game()                
                self.helpmenu.setOption(None)
                
            pygame.display.update()
        
if __name__ == '__main__' :
    main = Main()
    main.process()